package com.sky.apsect;

import com.sky.constant.AutoFillConstant;
import com.sky.context.BaseContext;
import com.sky.enumeration.OperationType;
import com.sky.exception.BaseException;
import io.jsonwebtoken.lang.Collections;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.util.ArrayUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.time.LocalDateTime;
import java.util.Objects;

/**
 * 公共字段自动填充切面类
 */
@Component
@Aspect
@Slf4j
public class AutoFillAspect {

    /**
     * 前置通知：增强新增和更新的业务方法
     */
    @Before("execution(* com.sky.mapper.*.*(..)) && @annotation(com.sky.apsect.AutoFill)")
    public void autoFill(JoinPoint joinPoint){
        String name = joinPoint.getSignature().getName();
        log.info("进入通知...{}", name);
        Object[] args = joinPoint.getArgs();
        Object entity = null;
        //防止为空
        if(Objects.isNull(args) || args.length <= 0){
            return;
        }
        entity = args[0];

        //获取AutoFill注解
        MethodSignature methodSignature = (MethodSignature)joinPoint.getSignature();
        AutoFill annotation = methodSignature.getMethod().getAnnotation(AutoFill.class);
        //添加操作，填充四个数据updateTime、updateUser、createTime、createUser

        if(annotation.value().equals(OperationType.INSERT)){
            try {
                Method setUpdateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                Method setCreateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);
                Method setUpdateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
                Method setCreateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_USER, Long.class);
                setUpdateTime.invoke(entity, LocalDateTime.now());
                setCreateTime.invoke(entity, LocalDateTime.now());
                setCreateUser.invoke(entity, BaseContext.getCurrentId());
                setUpdateUser.invoke(entity, BaseContext.getCurrentId());
            } catch (Exception e) {
                log.error("补充基础属性异常：{}",e.getMessage());
            }
        }else{
            //修改操作，填充updateTime、updateUser
            try {
                Method setUpdateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                Method setUpdateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
                setUpdateTime.invoke(entity, LocalDateTime.now());
                setUpdateUser.invoke(entity, BaseContext.getCurrentId());
            } catch (Exception e) {
                log.error("补充基础属性异常：{}",e.getMessage());
            }
        }
        log.info("公共字段填充成功  {}", entity.toString());
    }
}
